<template>
	<view>
		<view class="con_list">
			<view class="u-flex u-row-between" style="padding:20rpx 0;">
				<text style="color:#666;">选择方式</text>
				<text v-if="detail.type==2">借用物资</text>
				<text v-else>领用物资</text>
			</view>
			<view class="u-flex u-row-between" style="padding:20rpx 0;">
				<text style="color:#666;">使用时间</text>
				<text>{{detail.time}}</text>
			</view>
		</view>
		<view class="u-padding-20" style="font-size: 32rpx;font-weight: bold;">借领物资信息</view>
		<view class="con_list">
			<view v-for="(item,index) in detail.tools" :key="index" class="u-border-bottom">
				<view class="u-flex u-row-between" style="padding:20rpx 0;">
					<text style="color:#666;">物资名称</text>
					<text>{{item.name}}</text>
				</view>
				<view class="u-flex u-row-between" style="padding:20rpx 0;">
					<text style="color:#666;">物资数量</text>
					<text>{{item.num}}</text>
				</view>
			</view>
		</view>
		<button class="l_btn" shape="circle" @click="submit">提交</button>
		<u-toast ref="uToast" :loading="true"></u-toast>
	</view>
</template>

<script>
	import {
		borrow,receive
	} from "@/service/assetService.js";
	export default {
		data () {
			return {
				detail:{
					type:'',
					time:'',
					nid:[],
					tools:[]
				}
			}
		},
		onLoad(options) {
			let item=JSON.parse(decodeURIComponent(options.item));
			
			this.detail.type=item.type;
			this.detail.time=item.time;
			this.detail.nid=item.nid;
			this.detail.tools=item.tools
			
		},
		methods:{
			submit(){
				if(this.detail.type==2){
					for(var i=0;i<this.detail.tools.length;i++){
						borrow({
							gid:this.detail.tools[i].nid,
							num:this.detail.tools[i].num,
							time:this.detail.time
						}).then(res=>{
							if(res.code==200){
								this.$refs.uToast.show({
									type: 'success',
									title:'提交成功',
									icon:false
								})
								setTimeout(function(){
									uni.navigateBack()
								},1000)
							}else{
								this.$refs.uToast.show({
									type: 'error',
									title: res.mag,
									icon:false
								})
							}
						})
					}
					
				}else{
					for(var i=0;i<this.detail.tools.length;i++){
						receive({
							gid:this.detail.tools[i].nid,
							num:this.detail.tools[i].num,
							time:this.detail.time
						}).then(res=>{
							if(res.code==200){
								this.$refs.uToast.show({
									type: 'success',
									title:'提交成功',
									icon:false
								})
								setTimeout(function(){
									uni.navigateBack()
								},1000)
							}else{
								this.$refs.uToast.show({
									type: 'error',
									title: res.mag,
									icon:false
								})
							}
						})
					}
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #f8f8f8;
	}
	.con_list{
		background-color: #fff;
		padding:20rpx;
		margin:20rpx 0;
	}
	.l_btn{
		position: fixed;
		bottom:50rpx;
		margin-left:5%;
		width:90%;
		height:70rpx !important;
		padding:0;
		line-height: 70rpx;
		border-radius: 35rpx;
		font-size: 30rpx;
		color:#fff;
		background-image: url('https://resourse.cnlhjt.com/upload/20220825/d1aa35c1d129137d9b12c8f34e59c5bc.png');
		background-size: 100% 70rpx;
		border:none !important;
	}
</style>